package ink.lovejinhu.leetcode;

import ink.lovejinhu.common.TreeNode;

public class Solution_0617 {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {

        this.dfs(root1, root2, root);
        return root;
    }
    TreeNode root=null;
    public void dfs(TreeNode root1, TreeNode root2, TreeNode root) {
        if (root1 == null && root2 == null) {
            root = null;
            return;
        }
        if (root1 == null && root2 != null) {
            root = new TreeNode(root2.val);
            dfs(null, root2.left, root.left);
            dfs(null, root2.right, root.right);
        } else if (root1 != null && root2 == null) {
            root = new TreeNode(root1.val);
            dfs(root1.left, null, root.left);
            dfs(root1.right, null, root.right);
        } else {
            root = new TreeNode(root2.val + root1.val);
            dfs(root1.left, root2.left, root.left);
            dfs(root1.right, root2.right, root.right);
        }
    }

    public TreeNode mergeTrees01(TreeNode root1, TreeNode root2) {
        if (root1 == null) {
                return root2;

        }
        if (root2==null){
            return root1;
        }
        TreeNode node=new TreeNode(root1.val+ root2.val);
        node.left=mergeTrees01(root1.left, root2.left);
        node.right=mergeTrees01(root1.right, root2.right);
        return node;
    }
}
